package com.simplenix.nicasio.admin.mb;

import com.simplenix.nicasio.admin.eb.Group;
import com.simplenix.nicasio.admin.featsecurity.AdminSecurity;
import com.simplenix.nicasio.def.Feature;
import com.simplenix.nicasio.def.Module;
import com.simplenix.nicasio.hmaint.HMaintException;
import com.simplenix.nicasio.hmaint.util.HMaintUtil;
import com.simplenix.nicasio.misc.AppException;
import com.simplenix.nicasio.misc.Internationalization;
import com.simplenix.nicasio.misc.StrUtil;
import com.simplenix.nicasio.persistence.HibernateUtil;
import com.simplenix.nicasio.sys.SystemDef;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

/**
 *
 * @author fronald
 */
public class DefinePermissions {

	private String groupId = "";

	private List<SelectItem> groups;

	private Map<Module, List<Map<Feature, String>>> modFeatures;

	/** Creates a new instance of DefinePermissions */
	public DefinePermissions() {
	}

	public Group getGroup() throws HMaintException {
		Transaction t = HibernateUtil.getCurrentSession().getTransaction();
		if (!t.isActive()) {
			t.begin();
		}
		Group g = (Group) HMaintUtil.findById(Group.class, this.groupId);
		t.commit();
		return g;
	}

	/**
	 * @return the groups
	 */
	public List<SelectItem> getGroups() {
		Transaction t = HibernateUtil.getCurrentSession().getTransaction();
		if (!t.isActive()) {
			t.begin();
		}
		Criteria c = HibernateUtil.getCurrentSession().createCriteria(Group.class);
		c.addOrder(Order.asc("name"));
		List<Group> g = c.list();
		t.commit();
		groups = new ArrayList<SelectItem>();
		for (Group gr : g) {
			SelectItem s = new SelectItem(Long.toString(gr.getGroupId()), gr.getName());
			groups.add(s);
		}
		return groups;
	}

	/**
	 * @return the modFeatures
	 */
	public Map<Module, List<Map<Feature, String>>> getModFeatures() throws AppException {
		Transaction t = HibernateUtil.getCurrentSession().getTransaction();
		if (!t.isActive()) {
			t.begin();
		}
		Group group = (Group) HMaintUtil.findById(Group.class, this.groupId);
		modFeatures = new HashMap<Module, List<Map<Feature, String>>>();
		for (Module module : SystemDef.getInstance().getModules().getModulesList()) {
			if (!StrUtil.nvl(module.getName()).equals("nicasio")) {
				List<Feature> features = SystemDef.getInstance().getModules().getFeaturesByModule(module).getFeatureList();
				Map<Feature, String> permissions = new HashMap();
				List<Map<Feature, String>> featPerm = new ArrayList();
				for (Feature feat : features) {
					permissions.put(feat, Group.haveAccess(group, module, feat) ? "Y" : "N");
				}
				featPerm.add(permissions);
				modFeatures.put(module, featPerm);
			}
		}
		t.commit();
		return modFeatures;
	}

	public String save() throws AppException {
		Transaction t = HibernateUtil.getCurrentSession().getTransaction();
		if (!t.isActive()) {
			t.begin();
		}
		Group group = (Group) HMaintUtil.findById(Group.class, this.groupId);
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		for (Module module : SystemDef.getInstance().getModules().getModulesList()) {
			if (!StrUtil.nvl(module.getName()).equals("nicasio")) {
				List<Feature> features = SystemDef.getInstance().getModules().getFeaturesByModule(module).getFeatureList();
				for (Feature feature : features) {
					String key = module.getName() + "." + feature.getName();
					if (request.getParameterMap().containsKey(key)) {
						String value = StrUtil.nvl(request.getParameter(key));
						boolean v = value.equals("Y") ? true : false;
						Group.setAccess(group, module, feature, v);
					}
				}
			}
		}
		t.commit();
		return "Y";
	}

	/**
	 * @return the groupId
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId the groupId to set
	 */
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

}
